﻿using Microsoft.EntityFrameworkCore;
using ProjectCore.Common;
using ProjectCore.Domain.DomainService;
using ProjectCore.Domain.Model.Entity;
using ProjectCore.Domain.Repository.Interfaces;
using ProjectCore.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace ProjectCore.Infrastructure.DomainService
{
    public class ProductTypeDomainService : IProductTypeDomainService
    {
        private readonly IBaseRepository<ProductType> baseRepository;
        private readonly MyContext myContext;
        public ProductTypeDomainService(IBaseRepository<ProductType> _baseRepository, MyContext _myContext)
        {
            baseRepository = _baseRepository;
            myContext = _myContext;
        }

        //反填
        public async Task<ProductType> GetProductType(int Id)
        {
            try
            {
                return await myContext.ProductTypes.FindAsync(Id);
            }
            catch (Exception) { throw; }
        }


        //获取商品分类集合
        public async Task<HeaderResult<List<ProductType>>> GetProductTypes(int pageIndex, int pageSize)
        {
            try
            {
                Expression<Func<ProductType, bool>> where = e => true;
                var productTypes = await baseRepository.LoadEntityListAsync(where, e => e.Sort, "asc", pageIndex, pageSize);
                int count = await baseRepository.GetEntitiesCountAsync(where);
                HeaderResult<List<ProductType>> result = new HeaderResult<List<ProductType>>()
                {
                    IsSucceed = true,
                    Result = productTypes,
                    Total = count,
                };
                return result;
            }
            catch (Exception)
            {

                throw;
            }

        }


        //逻辑删除
        public async Task<int> IsDel(int Id)
        {
            try
            {
                ProductType productType = myContext.ProductTypes.Where(x => x.ProTypeID.Equals(Id)).FirstOrDefault();
                productType.IsDeleted = true;
                return await myContext.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }

        }


        //修改
        public async Task<int> UptProductType(ProductType productType)
        {
            try
            {
                return await baseRepository.UpdateEntityAsync(productType);
            }
            catch (Exception) { throw; }
        }
    }
}
